<?php
 
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 karPenilaian_Student
	 karPenilaian_Student_add
	 karPenilaian_Student_edit
	 */
	class CommbookController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Karyawan";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("karyawan")){
				LoginUser::redirectToLoginPage("Karyawan");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		}
	  				
		public function actionIndex(){
			LoginUser::getPermission("karyawan_commbook");
			$this->subMenu = "Commbook";
			$this->layout ="//layouts/ly_blank";
			$karyawan = $this->loginUser ->karyawan;
			$department_id = $karyawan->department_id;	
			$department_name = $karyawan->department->name;					
			$karyawan_name = $karyawan->name;	
			$karyawan_id=$karyawan->id;				
						
 			$module= $this->loginUser ->module;
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE    group by department_id order by id");

			if ($departments == null){
				$department_id == "";
			}else{	
				$department_id = Yii::app()->request->getParam("department_id", $departments[0]->department_id);
			}
			
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$student_id = Yii::app()->request->getParam("student_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
			$absent_date = Yii::app()->request->getParam("absent_date","");				
			$absent_date_end = Yii::app()->request->getParam("absent_date_end","");							

			$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
			}			
		 
            $tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
			if ($tingkatans == null){
				$tingkatan_id == "";
			}else{
				$tingkatan_id = $tingkatan_id == "" ? $tingkatans[0]->tingkatan_id : $tingkatan_id;
			}
			
			$mcon= Yii::app()->db;						
			$kelass = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$tingkatan_id,":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}
			
	 
		 
			$data=array(
					"department_id"=>$department_id,
					"departments" => $departments,						
					"karyawan_name"=>$karyawan_name,
					"karyawan_id"=>$karyawan_id,										
					"tahun_ajaran_id"=>$tahun_ajaran_id,
					"tingkatans"=>$tingkatans,							
					"tingkatan_id"=>$tingkatan_id,					
					"tahunAjarans"=>$tahunAjarans,					
					"kelass"=>$kelass,
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"department_name"=>$department_name
					);				
			$this->render('index',$data);	
 		}
		
		public function actionByCommbook(){		
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","");			
			$mcon= Yii::app()->db;
			$sqlall='select  C.id as id, C.create_date, C.parent_id, C.karyawan_id, C.student_id as student_id,subject,message,last_sender,read_status, B.sid, B.name, VSKTA.department_id,VSKTA.tahun_ajaran_id, VSKTA.kelas_tahun_ajaran_id
FROM  `m_commbook` C 
left join m_student B on C.student_id=B.id 
left join v_student_kelas_tahun_ajaran  VSKTA on VSKTA.student_id = C.student_id  
where  C.id is not null and parent_id=0  ';
			$sqltask_count = $sqlall . " and  kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and C.karyawan_id=:karyawan_id  order by read_status desc";
			$mReportMonthCommands=$mcon->createCommand($sqltask_count);		
			$mReportMonthCommands->bindValue('kelas_tahun_ajaran_id',$kelas_tahun_ajaran_id); 		
			$mReportMonthCommands->bindValue('karyawan_id',$karyawan_id); 					
			$mReportMonthCommands=$mReportMonthCommands->queryAll();		
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mReportMonthCommands;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}		 
				
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_commbook_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$commbook = new MCommbook;
				$commbook->create_date = date("Y-m-d H:i:s");
				$commbook->update_date = date("Y-m-d H:i:s");					
				$commbook->parent_id = 0; 
				$commbook->karyawan_id = Yii::app()->request->getParam("karyawan_id",""); 
				$commbook->student_id = Yii::app()->request->getParam("student_id","");
				$commbook->subject = Yii::app()->request->getParam("subject","");
				$commbook->message = Yii::app()->request->getParam("message","");
				$commbook->last_sender = "Teacher";
				$commbook->read_status = 1;				 				
				if ($commbook->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($commbook->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("karyawan_commbook_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");	
				$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$id));							
				$commbook_update->update_date = date("Y-m-d H:i:s");
				$commbook_update->last_sender = "Teacher";
				$commbook_update->read_status = 1;				 				
				if ($commbook_update->save()){
					$commbook = new MCommbook;
					$commbook->create_date = date("Y-m-d H:i:s");
					$commbook->update_date = date("Y-m-d H:i:s");					
					$commbook->parent_id = $id; 
					$commbook->karyawan_id = Yii::app()->request->getParam("karyawan_id",""); 
					$commbook->student_id = Yii::app()->request->getParam("student_id","");
					$commbook->subject = "Re: ".Yii::app()->request->getParam("subject","");
					$commbook->message = Yii::app()->request->getParam("message","");
					$commbook->last_sender = "Teacher";
					$commbook->read_status = 1;				 				
					if ($commbook->save()){
						echo "<script>
							alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
							window.close();					
						</script>";
						exit;
					}else{
						print_r($commbook->getErrors());
					}			
				}	
			}else if ($op == "del"){
				if (!LoginUser::getPermission("karyawan_commbook_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_semester where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}

	public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","0");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","0");			
			$student_id = Yii::app()->request->getParam("student_id","0");	
			$student_sid = Yii::app()->request->getParam("student_sid","");	
 					
			$students =VStudentKelasTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id","order"=>"sid"));		
			if ($students == null){
				$student_id == "";
				$student_sid = "";	
			}else{
				$student_id = $student_id == "" ? $students[0]->student_id : $student_id;
				$student_sid =  $student_sid == "" ? $students[0]->sid : $student_sid;			
			}							
					 
			$data = array("op"=>"add","callback"=>"","id"=>0,"students"=>$students,"student_id"=>$student_id,"student_sid"=>$student_sid,"kelas_tahun_ajaran_id" =>$kelas_tahun_ajaran_id,"karyawan_id" =>$karyawan_id,"subject"=>"","message"=>"","is_active"=>"1","data_view"=>"");
			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_commbook_add");
				$data["op"] = "add";
				$data["callback"] = $callback;
			}else if($op =="edit"){				
				LoginUser::getPermission("karyawan_commbook_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;								
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				$data["data_view"]="<table class=message><tr><Td>From</td><td>Message</td></tr>";
				$rsTingkat = MCommbook::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
				$data["karyawan_id"] = $karyawan_id;				
				$data["subject"] = $rsTingkat[0]->subject;
				$data["student_id"] = $rsTingkat[0]->student_id;
				$student_id = $rsTingkat[0]->student_id;				
 				$last_sender_cek= $rsTingkat[0]->last_sender;
				$last_subject_cek= $rsTingkat[0]->subject;				
				$data["data_view"]=$data["data_view"]."<tr><td width=50  >".  date_format(date_create($rsTingkat[0]->create_date), 'd-M-Y').'<br>'.$rsTingkat[0]->last_sender."</td><td valign=top><b>".$rsTingkat[0]->subject."</b><br>".$rsTingkat[0]->message."</td></tr>";
				
				$students =VStudentKelasTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and student_id = $student_id","order"=>"sid"));		
				if ($students == null){
					$student_sid = "";	
				}else{
					$student_sid =  $student_sid == "" ? $students[0]->sid : $student_sid;			
				}		
				$data["student_sid"] =$student_sid; 									
				}	
				
				$rsTingkat = MCommbook::model()->findAll(array("condition"=>"parent_id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
					foreach ($rsTingkat as $rs_Tingkat){
						$data["data_view"]=$data["data_view"]."<tr><td width=50 >".date_format(date_create($rs_Tingkat["update_date"]), 'd-M-Y').'<br>'.$rs_Tingkat["last_sender"]."</td><td valign=top>".$rs_Tingkat["message"]."</td></tr>";
						$last_id_cek= $rs_Tingkat["id"];
					}
				}								
 				$data["data_view"]=$data["data_view"]."</table>";
					
				if ($last_sender_cek=="Parent"){	
					$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$id));							
					$commbook_update->update_date = date("Y-m-d H:i:s");
					$commbook_update->read_status = 0;		
					$commbook_update->subject = $last_subject_cek;									 				
						if ($commbook_update->save()){
							/*echo "<script>
								alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
								window.close();					
							</script>";
							exit;*/
						}else{
							print_r($commbook_update->getErrors());
						}	 
					$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$last_id_cek));							
					$commbook_update->update_date = date("Y-m-d H:i:s");
					$commbook_update->read_status = 0;		
					$commbook_update->subject = $last_subject_cek;									 				
					if ($commbook_update->save()){
						/*echo "<script>
							alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
							window.close();					
						</script>";
						exit;*/
					}else{
						print_r($commbook_update->getErrors());
					}	 					
				}
//				echo $data["data_view"];
//				exit;
				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
		
	protected function beforeRender($view){
		if ($this->subMenu != ""){
			$this->title = $this->mainMenu . " - " .$this->subMenu;
		}
		return true;
	}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>